//
//  CellForRecommendTableView.m
//  echo
//
//  Created by dllo on 16/1/28.
//  Copyright © 2016年 Danny. All rights reserved.
//

#define WIDTH [UIScreen mainScreen].bounds.size.width / 414
#define HEIGHT [UIScreen mainScreen].bounds.size.height / 736

#import "CellForRecommendTableView.h"
#import <SDWebImage/UIButton+WebCache.h>
#import "ModelForRecommendTableViewCell.h"
#import "MusicPlayerVC.h"
#import <DKNightVersion.h>

@interface CellForRecommendTableView ()

@end

@implementation CellForRecommendTableView

- (void)dealloc{
    [_viewOfBack release];
    [_buttonForLeft release];
    [_buttonForRight release];
    [_labelForLeft release];
    [_labelForLeftBottom release];
    [_labelForRight release];
    [_labelForRightBottom release];
    [_arrForData release];
    [super dealloc];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.viewOfBack = [[UIView alloc]init];
        [self.contentView addSubview:self.viewOfBack];
        [self.viewOfBack release];
        
        self.buttonForLeft = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.viewOfBack addSubview:self.buttonForLeft];
        
        self.buttonForRight = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.viewOfBack addSubview:self.buttonForRight];
        
        self.labelForLeft = [[UILabel alloc]init];
        [self.viewOfBack addSubview:self.labelForLeft];
        [self.labelForLeft release];

        self.labelForLeftBottom = [[UILabel alloc]init];
        [self.viewOfBack addSubview:self.labelForLeftBottom];
        [self.labelForLeftBottom release];

        self.labelForRight = [[UILabel alloc]init];
        [self.viewOfBack addSubview:self.labelForRight];
        [self.labelForRight release];
        
        self.labelForRightBottom = [[UILabel alloc]init];
        [self.viewOfBack addSubview:self.labelForRightBottom];
        [self.labelForRightBottom release];
        [self handleNightModel];
        
    }
    
    
    return self;
}

- (void)handleNightModel{
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"夜间模式"]) {
        
        [DKNightVersionManager nightFalling];
        
    } else {
        
        [DKNightVersionManager dawnComing];
    }
    
    self.viewOfBack.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor], [UIColor colorWithRed:17 / 256.f green:27 / 256.f blue:47 / 256.f alpha:1]);
    self.labelForLeft.dk_textColorPicker = DKColorWithColors([UIColor blackColor], [UIColor whiteColor]);
    self.labelForRight.dk_textColorPicker = DKColorWithColors([UIColor blackColor], [UIColor whiteColor]);
    self.labelForLeftBottom.dk_textColorPicker = DKColorWithColors([UIColor blackColor], [UIColor whiteColor]);
    self.labelForRightBottom.dk_textColorPicker = DKColorWithColors([UIColor blackColor], [UIColor whiteColor]);
}

- (void)passModelForCell:(NSArray *)arr{

    self.arrForData = [NSMutableArray arrayWithArray:arr];
    ModelForRecommendTableViewCell *model1 = [arr objectAtIndex:0];
    ModelForRecommendTableViewCell *model2 = [arr objectAtIndex:1];
    
    [self.buttonForLeft sd_setBackgroundImageWithURL:[NSURL URLWithString:model1.pic_750] forState:UIControlStateNormal];
    [self.buttonForRight sd_setBackgroundImageWithURL:[NSURL URLWithString:model2.pic_750] forState:UIControlStateNormal];
    
    [self.buttonForLeft addTarget:self action:@selector(handleButtonForLeftOfRecommend:) forControlEvents:UIControlEventTouchUpInside];
    [self.buttonForRight addTarget:self action:@selector(handleButtonForRightOfRecommend:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    
    
    self.labelForLeft.text = model1.name;
    self.labelForRight.text = model2.name;
    
    
    
    NSString *str1 = [model1.channel stringByAppendingString:@"   频道"];
    NSString *str2 = [model2.channel stringByAppendingString:@"   频道"];
    

    NSRange range1 = [str1 rangeOfString:model1.channel];
    NSMutableAttributedString *attribue1 = [[NSMutableAttributedString alloc]initWithString:str1];
    [attribue1 addAttributes:@{NSForegroundColorAttributeName: [UIColor greenColor]} range:range1];
    [self.labelForLeftBottom setText:str1];
    [self.labelForLeftBottom setAttributedText:attribue1];
    [attribue1 release];
    
    NSRange range2 = [str2 rangeOfString:model2.channel];
    NSMutableAttributedString *attribue2 = [[NSMutableAttributedString alloc]initWithString:str2];
    [attribue2 addAttributes:@{NSForegroundColorAttributeName: [UIColor greenColor]} range:range2];
    [self.labelForRightBottom setText:str2];
    [self.labelForRightBottom setAttributedText:attribue2];
    [attribue2 release];
}

- (void)handleButtonForLeftOfRecommend:(UIButton *)button{
    ModelForRecommendTableViewCell *model = [self.arrForData objectAtIndex:0];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"postModelForLeftOfRecommend" object:nil userInfo:@{@"model":model}];
}

- (void)handleButtonForRightOfRecommend:(UIButton *)button{
    ModelForRecommendTableViewCell *model = [self.arrForData objectAtIndex:1];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"postModelForRightOfRecommend" object:nil userInfo:@{@"model":model}];
}

- (void)layoutSubviews{
    
    [super layoutSubviews];
    
    self.viewOfBack.frame = self.contentView.frame;
    self.buttonForLeft.frame = CGRectMake(20 * WIDTH, 10 * HEIGHT, ([UIScreen mainScreen].bounds.size.width - 60 * WIDTH) / 2, ([UIScreen mainScreen].bounds.size.width - 60 * WIDTH) / 2);
    self.buttonForRight.frame = CGRectMake(([UIScreen mainScreen].bounds.size.width - 60 * WIDTH) / 2 + 40 * WIDTH, 10 * HEIGHT, ([UIScreen mainScreen].bounds.size.width - 60 * WIDTH) / 2, ([UIScreen mainScreen].bounds.size.width - 60 * WIDTH) / 2);
    
    self.labelForLeft.frame = CGRectMake(25 * WIDTH, ([UIScreen mainScreen].bounds.size.width - 60) / 2 + 15, ([UIScreen mainScreen].bounds.size.width - 60 * WIDTH) / 2 - 5 * WIDTH, 30 * HEIGHT);
    self.labelForLeft.font = [UIFont fontWithName:@"ArialUnicodeMS" size:16.0];
    
    self.labelForRight.frame = CGRectMake(([UIScreen mainScreen].bounds.size.width - 60 * WIDTH) / 2 + 45 * WIDTH, ([UIScreen mainScreen].bounds.size.width - 60 * WIDTH) / 2 + 15 * WIDTH, ([UIScreen mainScreen].bounds.size.width - 60 * WIDTH) / 2 - 5 * WIDTH, 30 * HEIGHT);
    self.labelForRight.font = [UIFont fontWithName:@"ArialUnicodeMS" size:16.0];
    
    self.labelForLeftBottom.frame = CGRectMake(25 * WIDTH, ([UIScreen mainScreen].bounds.size.width - 60 * WIDTH) / 2 + 10 * WIDTH + 35 * WIDTH, ([UIScreen mainScreen].bounds.size.width - 60 * WIDTH) / 2 - 5 * WIDTH, 25 * HEIGHT);
    self.labelForLeftBottom.font = [UIFont fontWithName:@"AppleGothic" size:15.0];

    
    self.labelForRightBottom.frame = CGRectMake(([UIScreen mainScreen].bounds.size.width - 60 * WIDTH) / 2 + 45 * WIDTH, ([UIScreen mainScreen].bounds.size.width - 60 * WIDTH) / 2 + 10 * WIDTH + 35 * WIDTH, ([UIScreen mainScreen].bounds.size.width - 60 * WIDTH) / 2 - 5 * WIDTH, 25 * HEIGHT);
    self.labelForRightBottom.font = [UIFont fontWithName:@"AppleGothic" size:15.0];


    
    
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
